package com.ewili.x.special.service.data.impl;

import com.alibaba.fastjson.JSON;
import com.ewili.framework.utils.HttpClientUtils;
import com.ewili.x.special.service.data.inter.SharePledgeService;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@Service
public class SharePledgeServiceImpl implements SharePledgeService {


    @Override
    public List<String> getSharePledgeDates() {
        String url = "http://dcfm.eastmoney.com/EM_MutiSvcExpandInterface/api/js/get?type=ZD_DATE&js=var%20dateData=(x)&token=70f12f2f4f091e459a279469fe49eca5&st=tdate&sr=-1&p=1&ps=999&rt=0.19018293474212933";
        String content = HttpClientUtils.getContent(url, 30000, 30000);
        content = content.replace("var dateData=", "");
        List<String> datesToParse = JSON.parseArray(content, String.class);
        List<String> dates = new ArrayList<>();
        for (String dateToParse : datesToParse) {
            dates.add(JSON.parseObject(dateToParse).get("tdate").toString().replace("T00:00:00", ""));
        }
        return dates;
    }

    @Override
    public void spideSharePledges(String date) {
        String url = "http://dcfm.eastmoney.com/EM_MutiSvcExpandInterface/api/js/get?type=ZD_QL_LB&token=70f12f2f4f091e459a279469fe49eca5&cmd=&st=zdf&sr=1&p=1&ps=1000000&js=var%20RbSuzuoP={pages:(tp),data:(x)}&filter=(tdate=%27" + date + "%27)";
        try {
            Document content = Jsoup.connect(url).get();
            String jsonContent = Jsoup.parse(content.toString()).text();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
